import { Component, Injector, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    BatchTaskServiceProxy,
    BatchDto,
    NameValueDtoOfInt32,
    DepartmentServiceProxy,
    ComboboxItemDto,
    PrintServiceProxy,
    PrintCommonDto,
    JJDList,
    PrintJJDDto
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'rw4',
    templateUrl: './rw4.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class RW4ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();
    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;
    printInput = new PrintCommonDto();
    departments = new Array<NameValueDtoOfInt32>();
    departmentValue: number | undefined;
    oldprint = '';
    batch = new BatchDto();
    jJDList=new Array<JJDList>();
    jJD=new JJDList();
    department = '';

    constructor(
        injector: Injector,
        private _departmentServiceProxy: DepartmentServiceProxy,
        private _taskServiceProxy: BatchTaskServiceProxy,
        private _printServiceProxy: PrintServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;

        this._departmentServiceProxy.getWorkshops().subscribe(result => {
            this.departments = result.items;
        });

        this._taskServiceProxy.getBatch(pipe.taskId).subscribe(result => {
            this.batch = result;
        });

        this._taskServiceProxy
            .getDefaultDeliveryTarget(pipe.taskId)
            .subscribe(result => {
                this.departmentValue = result;
            });
            this._printServiceProxy.getPrintDrow().subscribe(result => {
                // this.teamGroupName = result.groupName;
                this.teamGroups = result;
                this._printServiceProxy.getUserPrint().subscribe(result1 => {
                    if(result1!=null){
                        this.oldprint = result1.value;
                    }
                    this.teamGroupName = result1;
                });
            });
    }

    save(): void {
        this.saving = true;

        let self = this;

        this._taskServiceProxy
            .newDelivery(
                this.pipe.taskId,
                this.departmentValue,
                this.batch.certificateId,
                this.batch.certificateQuantity
            )
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe(result => {
                this.notify.success('生成交接单成功');
                this._taskServiceProxy
                    .getBatch(self.pipe.taskId)
                    .subscribe(result => {
                        this.batch = result;
                    });
            });
    }

    print(): void {
        console.log(this.batch);
        this.primengTableHelper.showLoadingIndicator();
        if ( this.oldprint != this.teamGroupName.value){
            this._printServiceProxy.createOrUpdateUserPrint(this.teamGroupName.value).subscribe(result=>{})
        }
            this.printInput.printJJDDto=new PrintJJDDto();
            this.printInput.printSet = this.teamGroupName.value;
            this.printInput.type = 'JiaoJieDan';
            this.printInput.printJJDDto.xxCheJian = this.batch.deliveryRecord.sourceId + '车间';
            this.printInput.printJJDDto.jsCheJian = this.batch.deliveryRecord.destinationId + '车间';
            this.printInput.printJJDDto.jjdid = this.batch.id.toString();
            // this.batch.task.batches.forEach(item => {});
            this.jJD.productAmount = this.batch.deliveryRecord.deliveredQuantity;
            this.jJD.barCode =  this.batch.certificateId;
            this.jJD.remark = this.batch.deliveryRecord.remark;
            this.jJD.drawingNum = this.batch.task.drawingCode;
            this.jJD.productName = this.batch.task.drawingName;
            this.jJD.taskNum = this.batch.task.taskCode;
            this.jJDList.push(this.jJD);
            this.printInput.printJJDDto.jjdList= this.jJDList;
            this._printServiceProxy.print(this.printInput).subscribe(result=>{
                this.notify.success('生成定制单成功');
                this.primengTableHelper.hideLoadingIndicator();
            })
    }
}
